package com.seex.motor;

import android.os.Handler;
import android.util.Log;


/**
 * Created by henry  16/5/3.
 */
public class Motor {

    private final String TAG = Motor.class.getSimpleName();

    //运动状态
    private boolean running = true;
    //电机是否开启
    private boolean isOpen = false;
    private int mFd = -1;
    public static final int MSG_POINT_END = 1001;
    public static final int MSG_POINT_START = 2001;
    public static final int MSG_RUN_START = 3001;
    public static final int MSG_RUN_STOP = 3002;

    private CycleRunThread mCycleRunThread;

    /**
     * 消息回传
     */
    public Handler mHandler;

    public void setHandler(Handler handler) {
        mHandler = handler;
    }


    private void sendMsg(int what) {
        if (null != mHandler) {
          mHandler.sendEmptyMessage(what);
        }

        if (null != mListener) {
            mListener.onStatus(what);
        }
    }

    /**
     * 开启电机
     */
    public boolean open() {
        if (!isOpen) {
            mFd = MotorController.openMotor();
            printLog("----open:----fd = " + mFd);
            isOpen = mFd > 0;
        }
        if (isOpen) {
            //enableKey(false);
        }
        return isOpen;
    }

    /**
     * 关闭电机
     */
    public void close() {

        if (running) {
            stop();
        }

        isOpen = false;

        printLog("----close----");
        enableKey(true);
        netStop();
        MotorController.closeMotor(mFd);
    }

    /**
     * 启动
     */
    @Deprecated
    public void start() {

        printLog("----start----");

        sendMsg(MSG_RUN_START);

        running = true;

        MotorController.startMotor(mFd);
    }

    /**
     * 启动
     */
    public void netStart() {
        MotorController.netStart(mFd);
       // enableKey(false);
    }


    /**
     * 停止
     */
    public void netStop() {
        MotorController.netStop(mFd);
       // enableKey(true);
    }


    /**
     * todo 优先级先关闭
     * 控制物理按键是否生效
     */
    public void enableKey(boolean enable) {
        if (enable) {
            MotorController.enableKey(mFd);
        } else {
            MotorController.disableKey(mFd);
        }
    }

    /**
     * 停止
     */
    @Deprecated
    public void stop() {


        sendMsg(MSG_RUN_STOP);


        printLog("----stop----");

        running = false;

        MotorController.stopMotor(mFd);

        if (null != mCycleRunThread) {
            mCycleRunThread.setRunning(false);
        }
    }


    /**
     * <<<反向运动
     */
    public void reverseRun() {

        if (isOpen) {


            new Thread(new Runnable() {
                @Override
                public void run() {

                    Motor.this.start();

                    int status = MotorController.getStatus(mFd);

                    printLog("----ReverseRun:----" + status);

                    while (status != 0) {
                        if (running) {
                            MotorController.reverseRun(mFd);
                            printLog("->->->->->\n");
                            status = MotorController.getStatus(mFd);
                        }
                    }

                    sendMsg(MSG_POINT_START);


                    Motor.this.stop();
                }
            }).start();


        }
    }

    /**
     * >>>正向运动
     */
    public void forwardRun() {

        if (isOpen) {


            new Thread(new Runnable() {
                @Override
                public void run() {

                    Motor.this.start();

                    int status = MotorController.getStatus(mFd);

                    printLog("----forwardRun:----" + status);

                    while (status != 1) {
                        if (running) {
                            MotorController.forwardRun(mFd);
                            printLog("->->->->->\n");
                            status = MotorController.getStatus(mFd);
                        }
                    }
                    sendMsg(MSG_POINT_END);

                    Motor.this.stop();

                }
            }).start();

        }
    }


    /**
     * 循环运动
     */
    @Deprecated
    public void startCycleRun() {

        printLog("----startCycleRun:----");

        if (isOpen) {
            if (null != mCycleRunThread) {
                mCycleRunThread.setRunning(false);
                mCycleRunThread = null;
            }
            mCycleRunThread = new CycleRunThread(true);
            mCycleRunThread.start();
        }


    }


    /**
     * 循环转动线程
     */
    class CycleRunThread extends Thread {
        public boolean isRunning;

        public CycleRunThread(boolean isRunning) {
            this.isRunning = isRunning;
        }

        public void setRunning(boolean running) {
            isRunning = running;
        }

        @Override
        public void run() {
            super.run();

            Motor.this.start();

            int status = MotorController.getStatus(mFd);

            //运行中的方向
            boolean forward = false;

            while (isRunning) {

                //到达起点
                if (status == 0) {
                    forward = true;
                    MotorController.forwardRun(mFd);
                    printLog("0---->\n");
                }
                //到达终点
                else if (status == 1) {
                    forward = false;
                    MotorController.reverseRun(mFd);
                    printLog("<----1\n");
                }
                //运行过程中
                else {

                    if (forward) {
                        MotorController.forwardRun(mFd);
                        printLog("->->->->->\n");
                    } else {
                        MotorController.reverseRun(mFd);
                        printLog("<-<-<-<-<-\n");
                    }
                }
                status = MotorController.getStatus(mFd);
            }


        }

    }

    private void keepDevicesOn() {
        boolean open = open();
        while (!open) {
            open = open();
        }
    }

    private void printLog(String msg) {
        Log.e(TAG, msg);
    }

    public interface StatusListener {
        void onStatus(int status);

    }

    private StatusListener mListener;

    public void setStatusListener(StatusListener mListener) {
        this.mListener = mListener;
    }
}
